package net.wanho.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.PurchaseDTO;
import net.wanho.dto.PurchaseaddDTO;
import net.wanho.mapper.BuyListMapper;
import net.wanho.mapper.InStoreMapper;
import net.wanho.mapper.ProductMapper;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.po.BuyList;
import net.wanho.po.InStore;
import net.wanho.po.Product;
import net.wanho.service.PurchaseService;
import net.wanho.util.ThreadUtil;
import net.wanho.vo.BuyListVo;
import net.wanho.vo.PageInfo;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class PurchaseServiceImpl extends ServiceImpl<ProductMapper, Product> implements PurchaseService {

    private final BuyListMapper buyListMapper;
    private final InStoreMapper  inStoreMapper;
    private final UserInfoMapper userInfoMapper;

    @Override
    public PageInfo<BuyListVo> findPageList(Integer pageNum, Integer pageSize, PurchaseDTO purchaseDTO) {
        Page<BuyListVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BuyListVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!ObjectUtil.isEmpty(purchaseDTO.getStoreId()), "b.store_id", purchaseDTO.getStoreId());
        queryWrapper.like(!ObjectUtil.isEmpty(purchaseDTO.getProductName()), "p.product_name", purchaseDTO.getProductName());
        queryWrapper.lt(!ObjectUtil.isEmpty(purchaseDTO.getStartTime()), "b.create_time", purchaseDTO.getStartTime());
        queryWrapper.gt(!ObjectUtil.isEmpty(purchaseDTO.getEndTime()), "b.create_time", purchaseDTO.getEndTime());
        queryWrapper.like(!ObjectUtil.isEmpty(purchaseDTO.getBuyUser()), "b.buy_user", purchaseDTO.getBuyUser());
        queryWrapper.eq(!ObjectUtil.isEmpty(purchaseDTO.getIsIn()), "b.is_in", purchaseDTO.getIsIn());

        Page<BuyListVo> p = buyListMapper.selectListByPage(page, queryWrapper);
        PageInfo<BuyListVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setLimitIndex(page.offset());
        pageInfo.setResultList(p.getRecords());
        return pageInfo;
    }

    @Override
    public void save(PurchaseaddDTO purchaseaddDTO) {
        BuyList buyList = new BuyList();
        buyList.setStoreId(purchaseaddDTO.getStoreId());
        buyList.setProductId(purchaseaddDTO.getProductId());
        buyList.setBuyNum(purchaseaddDTO.getBuyNum());
        buyList.setFactBuyNum(purchaseaddDTO.getBuyNum());
        buyList.setBuyTime(new Date());
        buyList.setSupplyId(purchaseaddDTO.getSupplyId());
        buyList.setPlaceId(purchaseaddDTO.getPlaceId());
        buyList.setBuyUser(purchaseaddDTO.getBuyUser());
        buyList.setPhone(purchaseaddDTO.getPhone());
        buyList.setIsIn(0);
        buyListMapper.insert(buyList);
    }

    @Override
    public void saverecord(BuyListVo buyListVo) {
        BuyList  buyList = new BuyList();
        buyList.setBuyId(buyListVo.getBuyId());
        buyList.setIsIn(1);
        buyListMapper.updateById(buyList);
        InStore  inStore = new InStore();
        inStore.setProductId(buyListVo.getProductId());
        inStore.setStoreId(buyListVo.getStoreId());
        inStore.setInNum(buyListVo.getFactBuyNum());
        inStore.setCreateTime(new Date());
        inStore.setIsIn(0);
        inStore.setCreateBy(ThreadUtil.getId());
        QueryWrapper<InStore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!ObjectUtil.isEmpty(inStore.getProductId()),"product_id", inStore.getProductId());
        queryWrapper.eq(!ObjectUtil.isEmpty(inStore.getStoreId()),"store_id", inStore.getStoreId());
        queryWrapper.eq(!ObjectUtil.isEmpty(inStore.getIsIn()),"is_in", inStore.getIsIn());
        if (inStoreMapper.selectOne(queryWrapper) != null) {
            inStore.setInNum(inStore.getInNum() + inStoreMapper.selectOne(queryWrapper).getInNum());
            inStoreMapper.update(inStore, queryWrapper);
        }else
        inStoreMapper.insert(inStore);
    }

    @Override
    public void removeById(Integer id) {
        buyListMapper.deleteById(id);
    }

    @Override
    public void update(PurchaseaddDTO purchaseaddDTO) {
        BuyList buyList = new BuyList();
        buyList.setStoreId(purchaseaddDTO.getStoreId());
        buyList.setBuyNum(purchaseaddDTO.getBuyNum());
        buyList.setFactBuyNum(purchaseaddDTO.getFactBuyNum());
        buyListMapper.update(buyList, null);
    }
}
